import EventMgr from "../../../Core/Event/EventMgr";
import Log from "../../../Core/Log/Log";
import SdkTool from "../../../Core/Sdk/SdkTool";
import TimerMgr from "../../../Core/Timer/TimerMgr";
import ConfigManager from "../../Config/ConfigManager";
import ProtoBufManager from "../../proto/ProtoBufManager";
import { common, support } from "../../proto/protoCmd";
import { ViewManager } from "../base/ViewManager";
import { AbstractModel } from "../socket/AbstractModel";
import { GameModels } from "../socket/GameModels";
import { SocketManager } from "../socket/SocketManager";
import { ItemVO } from "../vo/ItemVO";
import ModuleWindow from "../window/ModuleWindow";
import WindowObject from "../window/WindowObject";

export class ModelSupport extends AbstractModel {
    /**新手引导奖励是否领取0未领 1已领 */
    public guideReward: number;
    /**每日奖励领取时间戳 */
    public dailyRewardSign: number;
    /**抽奖次数 */
    public luckyDrawCount: number;
    /**是否领取侧边栏奖励0未领 1已领 */
    public isSide: number;
    /**战绩 */
    public send_ReqGameLogs(): void {
        const msg = ProtoBufManager.ins.support.ReqGameLogs.create();
        msg.page = 1;
        const data = ProtoBufManager.ins.support.ReqGameLogs.encode(msg).finish();
        this.send(4007, `Support.Support.ReqGameLogs`, data);
    }
    /**领取侧边栏奖励 */
    public send_ReqDouYinSide(): void {
        const msg = ProtoBufManager.ins.support.ReqDouYinSide.create();
        const data = ProtoBufManager.ins.support.ReqDouYinSide.encode(msg).finish();
        this.send(4006, `Support.Support.ReqDouYinSide`, data);
    }
    /**
     * 发送领取抽奖奖励请求
     * @param rewardid 奖励ID
     * @param ad 广告位标识，默认为0
     */
    public send_ReqTakeLuckyDrawAward(ad: number = 0): void {
        const msg = ProtoBufManager.ins.support.ReqTakeLuckyDrawAward.create();
        msg.IsAD = ad;
        const data = ProtoBufManager.ins.support.ReqTakeLuckyDrawAward.encode(msg).finish();
        this.send(4005, `Support.Support.ReqTakeLuckyDrawAward`, data);
    }
    /**
     * 发送领取日常奖励的请求
     * @param ad 广告标识，默认为0
     */
    public send_ReqTakeDailyAward(ad: number = 0): void {
        const msg = ProtoBufManager.ins.support.ReqTakeDailyAward.create();
        msg.IsAD = ad;
        const data = ProtoBufManager.ins.support.ReqTakeDailyAward.encode(msg).finish();
        this.send(4004, `Support.Support.ReqTakeDailyAward`, data);
    }
    /**
     * 发送奖励信息查询
     */
    public send_ReqAwardInfo(): void {
        const msg = ProtoBufManager.ins.support.ReqAwardInfo.create();
        const data = ProtoBufManager.ins.support.ReqAwardInfo.encode(msg).finish();
        this.send(4003, `Support.Support.ReqAwardInfo`, data);
    }
    /**
     * 发送完成引导请求
     * @param ad 广告标志，默认为0
     */
    public send_ReqFinishGuide(ad: number = 0): void {
        const msg = ProtoBufManager.ins.support.ReqFinishGuide.create();
        msg.IsAD = ad;
        const data = ProtoBufManager.ins.support.ReqFinishGuide.encode(msg).finish();
        this.send(4002, `Support.Support.ReqFinishGuide`, data);
    }
    /**请求道具列表 */
    public send_ReqGetItemBagList(): void {
        const msg = ProtoBufManager.ins.support.ReqGetItemBagList.create();
        const data = ProtoBufManager.ins.support.ReqGetItemBagList.encode(msg).finish();
        this.send(4001, `Support.Support.ReqGetItemBagList`, data);
    }
    /**请求排行榜 */
    public send_ReqRankInfos(index?: number): void {
        const msg = ProtoBufManager.ins.support.ReqRankInfos.create();
        msg.PageIndex = index;
        const data = ProtoBufManager.ins.support.ReqRankInfos.encode(msg).finish();
        this.send(4000, `Support.Support.ReqRankInfos`, data);
        ViewManager.ins.showloading();
    }
    protected registerInterestedProtocols(): void {
        this.reg(4000, this.onRespRankInfos, `support`, `RespRankInfos`);
        this.reg(4001, this.onRespGetItemBagList, `support`, `RespGetItemBagList`);
        this.reg(4002, this.onRespFinishGuide, `support`, `RespFinishGuide`);
        this.reg(4003, this.onRespAwardInfo, `support`, `RespAwardInfo`);
        this.reg(4004, this.onRespTakeDailyAward, `support`, `RespTakeDailyAward`);
        this.reg(4005, this.onRespTakeLuckyDrawAward, `support`, `RespTakeLuckyDrawAward`);
        this.reg(4006, this.onRespDouYinSide, `support`, `RespDouYinSide`);
        this.reg(4007, this.onRespGameLogs, `support`, `RespGameLogs`);
    }
    private onRespGameLogs(info: support.RespGameLogs): void {
        EventMgr.emit(`event_RespGameLogs`, info.List);
    }
    private onRespDouYinSide(info: support.RespDouYinSide): void {
        for (let i = 0; i < info.AwardList.length; i++) {
            const cfg = ConfigManager.tables.TbGameItem.get(info.AwardList[i].Id);
            TimerMgr.ins.add(500 * i, 1, () => {
                ViewManager.ins.showtips(`领取了${cfg.name}*${info.AwardList[i].Count}`);
            }, this);
            for (let j in GameModels.bag.skillItem) {
                if (GameModels.bag.skillItem[j]) {
                    if (GameModels.bag.skillItem[j].cfg.id === cfg.id) {
                        GameModels.bag.skillItem[j].count += info.AwardList[i].Count;
                    }
                }
            }
        }
        this.send_ReqAwardInfo();
        EventMgr.emit(`event_RespDouYinSide`, info);
    }
    private onRespTakeLuckyDrawAward(info: support.RespTakeLuckyDrawAward): void {
        for (let i = 0; i < info.AwardList.length; i++) {
            const cfg = ConfigManager.tables.TbGameItem.get(info.AwardList[i].Id);
            // TimerMgr.ins.add(500 * i, 1, () => {
            //     ViewManager.ins.showtips(`恭喜你获得${cfg.name}*${info.AwardList[i].Count}`);
            // }, this);
            for (let j in GameModels.bag.skillItem) {
                if (GameModels.bag.skillItem[j]) {
                    if (GameModels.bag.skillItem[j].cfg.id === cfg.id) {
                        GameModels.bag.skillItem[j].count += info.AwardList[i].Count;
                    }
                }
            }
        }
        this.send_ReqAwardInfo();
        EventMgr.emit(`event_RespTakeLuckyDrawAward`, info);
    }
    private onRespTakeDailyAward(info: support.RespTakeDailyAward): void {
        for (let i = 0; i < info.AwardList.length; i++) {
            const cfg = ConfigManager.tables.TbGameItem.get(info.AwardList[i].Id);
            TimerMgr.ins.add(500 * i, 1, () => {
                ViewManager.ins.showtips(`领取了${cfg.name}*${info.AwardList[i].Count}`);
            }, this);
            for (let j in GameModels.bag.skillItem) {
                if (GameModels.bag.skillItem[j]) {
                    if (GameModels.bag.skillItem[j].cfg.id === cfg.id) {
                        GameModels.bag.skillItem[j].count += info.AwardList[i].Count;
                    }
                }
            }
        }
        this.send_ReqAwardInfo();
        EventMgr.emit(`event_RespTakeDailyAward`, info);
    }

    private onRespAwardInfo(info: support.RespAwardInfo): void {
        this.guideReward = info.IsGuide | 0;
        this.isSide = info.IsSide | 0;
        Log.ins.log(1, `侧边栏奖励${this.isSide == 0 ? `未领` : `已领`}`);
        this.dailyRewardSign = info.DailyTime | 0;
        this.luckyDrawCount = info.LuckyDrawCount | 0;
        EventMgr.emit(`event_RespAwardInfo`, info);
    }
    private onRespFinishGuide(info: support.RespFinishGuide): void {
        for (let i = 0; i < info.AwardList.length; i++) {
            const cfg = ConfigManager.tables.TbGameItem.get(info.AwardList[i].Id);
            TimerMgr.ins.add(500 * i, 1, () => {
                ViewManager.ins.showtips(`领取了${cfg.name}*${info.AwardList[i].Count}`);
            }, this);
            for (let j in GameModels.bag.skillItem) {
                if (GameModels.bag.skillItem[j]) {
                    if (GameModels.bag.skillItem[j].cfg.id === cfg.id) {
                        GameModels.bag.skillItem[j].count += info.AwardList[i].Count;
                    }
                }
            }
        }
        this.send_ReqAwardInfo();
        EventMgr.emit(`event_RespFinishGuide`, info);
    }
    private onRespGetItemBagList(info: support.RespGetItemBagList): void {
        if (!info.List || info.List.length <= 0) {
            return;
        }
        for (let i of info.List) {
            for (let j in GameModels.bag.skillItem) {
                if (GameModels.bag.skillItem[j]) {
                    if (GameModels.bag.skillItem[j].cfg.id == i.Id) {
                        GameModels.bag.skillItem[j].count = i.Count;
                    }
                }
            }
        }
    }

    private onRespRankInfos(info: support.RespRankInfos): void {
        ViewManager.ins.removeLoading();
        if (info.Datas.length <= 0) {
            return;
        }
        EventMgr.emit(`event_RespRankInfos`, info);
    }
}